package cn.fzkj.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.fzkj.gulimall.product.entity.AttrEntity;
import cn.fzkj.gulimall.product.service.AttrAttrgroupRelationService;
import cn.fzkj.gulimall.product.service.AttrService;
import cn.fzkj.gulimall.product.service.CategoryService;
import cn.fzkj.gulimall.product.vo.AttrGroupAndCascadeAttrsVo;
import cn.fzkj.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.fzkj.gulimall.product.entity.AttrGroupEntity;
import cn.fzkj.gulimall.product.service.AttrGroupService;
import cn.fzkj.common.utils.PageUtils;
import cn.fzkj.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 属性分组
 *
 * @author chenxin
 * @email chenxin@gmail.com
 * @date 2022-02-19 22:47:32
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 查询分组关联的属性
     */
    @GetMapping(value = "{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){

        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 查询分组未关联的属性
     */
    @GetMapping(value = "{attrGroupId}/attr/norelation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttrPage(attrGroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 新建属性分组关联关系
     */
    @PostMapping(value = "attr/relation")
    public R saveAttrGroupRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        boolean save = relationService.saveBatch(attrGroupRelationVo);
        return R.ok().put("ok", save);
    }

    /**
     * 获取分类下所有分组 & 关联的属性信息
     */
    @GetMapping(value = "/{catelogId}/withattr")
    public R getAttrGroupAndCascadeAttrsByCatelogId(@PathVariable("catelogId") Long catelogId){

        List<AttrGroupAndCascadeAttrsVo> attrGroupAndCascadeAttrs = attrGroupService.getAttrGroupAndCascadeAttrByCatelogId(catelogId);
        return R.ok().put("data", attrGroupAndCascadeAttrs);
    }

    /**
     * 删除分组和属性的关联关系
     */
    @PostMapping(value = "attr/relation/delete")
    public R relationDelete(@RequestBody List<AttrGroupRelationVo> relationVos){
        boolean success = relationService.deleteAttrGroupRelations(relationVos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		Long[] path = categoryService.findCategoryPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(path);

        return R.ok().put("data", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
